/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import junit.framework.TestCase;
import net.sourceforge.stripes.validation.ValidationError;
import org.xptracker.util.BetterDateTypeConverter;

import java.text.SimpleDateFormat;
import java.util.*;

public class BetterDateTypeConverterTest extends TestCase {
  public void testFormats() throws Exception {
    Date fullDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2007-01-02 22:45:15");
    Date noSeconds = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2007-01-02 22:45");
    Date noTime = new SimpleDateFormat("yyyy-MM-dd").parse("2007-01-02");

    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, 22);
    c.set(Calendar.MINUTE, 45);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    Date timeOnlyNoSeconds = c.getTime();

    c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, 22);
    c.set(Calendar.MINUTE, 45);
    c.set(Calendar.SECOND, 15);
    c.set(Calendar.MILLISECOND, 0);
    Date timeOnly = c.getTime();

    BetterDateTypeConverter converter = new BetterDateTypeConverter();
    converter.setLocale(Locale.getDefault());

    Set<ValidationError> errors = Collections.emptySet();

    assertEquals(normalize(new Date()), normalize(converter.convert("now", Date.class, errors)));

    assertEquals(noTime, converter.convert("1 2 07", Date.class, errors));
    assertEquals(noTime, converter.convert("Jan 2 2007", Date.class, errors));
    assertEquals(noTime, converter.convert("January 2, 2007", Date.class, errors));
    assertEquals(noTime, converter.convert("2007 01 02", Date.class, errors));
    assertEquals(noTime, converter.convert("2007-01-02", Date.class, errors));
    assertEquals(noTime, converter.convert("2007/01/02", Date.class, errors));
    assertEquals(noTime, converter.convert("2007 Jan 02", Date.class, errors));
    assertEquals(noTime, converter.convert("2007-Jan-02", Date.class, errors));
    assertEquals(noTime, converter.convert("2007-/Jan/02", Date.class, errors));

    assertEquals(fullDate, converter.convert(fullDate.toString(), Date.class, errors));

    assertEquals(noSeconds, converter.convert("Jan 2, 2007 10:45 PM", Date.class, errors));
    assertEquals(noSeconds, converter.convert("Jan 2, 2007 22:45", Date.class, errors));
    assertEquals(fullDate, converter.convert("Jan 2, 2007 10:45:15 PM", Date.class, errors));
    assertEquals(fullDate, converter.convert("Jan 2, 2007 22:45:15", Date.class, errors));
    assertEquals(fullDate, converter.convert("Jan 2 2007 22:45:15", Date.class, errors));

    assertEquals(noSeconds, converter.convert("January 2, 2007 10:45 PM", Date.class, errors));
    assertEquals(noSeconds, converter.convert("January 2, 2007 22:45", Date.class, errors));
    assertEquals(fullDate, converter.convert("January 2, 2007 10:45:15 PM", Date.class, errors));
    assertEquals(fullDate, converter.convert("January 2, 2007 22:45:15", Date.class, errors));
    assertEquals(fullDate, converter.convert("January 2, 2007 22:45:15", Date.class, errors));

    assertEquals(noSeconds, converter.convert("2007 01 02 10:45 PM", Date.class, errors));
    assertEquals(noSeconds, converter.convert("2007-01-02 10:45 PM", Date.class, errors));
    assertEquals(noSeconds, converter.convert("2007/01/02 10:45 PM", Date.class, errors));
    assertEquals(noSeconds, converter.convert("2007-01-02 22:45", Date.class, errors));
    assertEquals(fullDate, converter.convert("2007-01-02 10:45:15 PM", Date.class, errors));
    assertEquals(fullDate, converter.convert("2007-01-02 22:45:15", Date.class, errors));

    assertEquals(timeOnlyNoSeconds, converter.convert("10:45 PM", Date.class, errors));
    assertEquals(timeOnlyNoSeconds, converter.convert("22:45", Date.class, errors));
    assertEquals(timeOnly, converter.convert("10:45:15 PM", Date.class, errors));
    assertEquals(timeOnly, converter.convert("10:45:15 pM", Date.class, errors));
    assertEquals(timeOnly, converter.convert("22:45:15 ", Date.class, errors));
  }

  private Date normalize(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTime();
  }
}
